package com.ebupt.migu.music.userGroup.service.impl;

import com.ebupt.migu.common.pojo.CustomerStatusRequestBean;
import com.ebupt.migu.music.strategy.entity.StrategyInfo;
import com.ebupt.migu.music.userGroup.entity.UserGroupInfo;
import com.ebupt.migu.music.userGroup.mapper.CrowdTaskMapper;
import com.ebupt.migu.music.userGroup.service.CrowdTaskService;
import com.ebupt.migu.restapi.RestHttpPost;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人群定时任务
 * User: lizm
 * Date:  2019/10/16
 */
@Slf4j
@Service
public class CrowdTaskServiceImpl implements CrowdTaskService {

    @Autowired
    private CrowdTaskMapper crowdMapper;
    @Value("${label.prefix}")
    private String LABEL_PREFIX;
    @Value("${label.customer_status}")
    private String CUSTOMER_STATUS;

    @Override
    public void autoUpdateCrowdStatus() {
        List<HashMap<String, String>> groups = crowdMapper.queryAllGroups();
        log.info("等待自动更新的客群：{}", groups.size());
        for (HashMap<String, String> group : groups) {
            String groupId = group.get("groupId");
            if (null != group.get("isStaticCustomer") && "1".equals(String.valueOf(group.get("isStaticCustomer")))) {
                log.info("自动更新上报：{}", groupId);
                customerStatus(groupId, 1);
            }
            crowdMapper.autoUpdateCrowdStatus(group);
        }
        crowdMapper.deleteTmpGroups();
    }

    @Override
    public List<UserGroupInfo> queryCrowdList() {
        return crowdMapper.queryCrowdList();
    }

    @Override
    public List<UserGroupInfo> queryCrowdRegroupList(String groupId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        return crowdMapper.queryCrowdRegroupList(paramMap);
    }

    @Override
    public List<StrategyInfo> queryStrategyAdoptedList(String groupId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        return crowdMapper.queryStrategyAdoptedList(paramMap);
    }

    @Override
    public int updateCrowdAutoUpdateState(String groupId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        int i = crowdMapper.updateCrowdAutoUpdateState(paramMap);
        return i;
    }

    @Override
    public void updateUserGroupInfo(String gid, int status) {
        Map<String, Object> map = new HashMap<>();
        map.put("gid", gid);
        map.put("status", status);
        crowdMapper.updateUserGroupInfo(map);
    }

    private void customerStatus(String groupId, int status) {
        CustomerStatusRequestBean bean = new CustomerStatusRequestBean();
        bean.setCustomerId(groupId);
        bean.setStatus(status);
        log.info("自动更新上报信息：{}", bean.toString());
        RestHttpPost post = new RestHttpPost();
        post.doRest(bean, LABEL_PREFIX + CUSTOMER_STATUS);
    }
}
